package com.example.rabbitmqdemo.simple_queue;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Recv {
    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUsername("root");
        connectionFactory.setPassword("Albb123.");
        connectionFactory.setHost("42.194.182.59");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare("hello",true,false,false,null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                // consumerTag 是固定的 可以做此会话的名字， deliveryTag 每次接收消息+1
                System.out.println("consumerTag消息标识="+consumerTag);
                //可以获取交换机，路由健等
                System.out.println("envelope元数据="+envelope);
                System.out.println("properties配置信息="+properties);
                System.out.println("body="+new String(body,"utf-8"));
            }
        };

        channel.basicConsume("hello",true,consumer);

    }
}
